import datetime
from dtos.result import Result
from libs.init_apscheduler import scheduler
from utils.common_util import CommonUtil


class SchedulerWrapper(object):

    def __init__(self, job_store="redis"):
        self.job_store = job_store

    @staticmethod
    def _get_args(args):
        return args.split(",") if args else []

    def add_job(self, func, minutes, job_id, name, mode="interval", args="", start_date=None, end_date=None):
        args = self._get_args(args)
        start_date = CommonUtil.convert_timestamp_to_dt(start_date) if start_date else None
        end_date = CommonUtil.convert_timestamp_to_dt(end_date) if end_date else None
        start_date = datetime.datetime.utcnow() if not start_date else start_date
        try:
            scheduler.add_job(func, mode, minutes=minutes, id=job_id, name=name, args=args,
                              jobstore=self.job_store, next_run_time=start_date, end_date=end_date)
            return Result.success_result()
        except Exception as ex:
            return Result.error_result(message=str(ex))
            
    def cron_add_job(self, func, hour, minute, job_id, name, mode="cron",
                     args="", start_date=None, end_date=None, misfire_grace_time=None):
        args = self._get_args(args)
        start_date = CommonUtil.convert_timestamp_to_dt(start_date) if start_date else None
        end_date = CommonUtil.convert_timestamp_to_dt(end_date) if end_date else None
        start_date = datetime.datetime.utcnow() if not start_date else start_date
        try:
            scheduler.add_job(func, mode, hour=hour, minute=minute, id=job_id, name=name, args=args,
                              jobstore=self.job_store, next_run_time=start_date, end_date=end_date,
                              misfire_grace_time=misfire_grace_time)
            return Result.success_result()
        except Exception as ex:
            return Result.error_result(message=str(ex))

    def remove_job(self, job_id):
        try:
            scheduler.remove_job(job_id=job_id, jobstore=self.job_store)
            return Result.success_result()
        except Exception as ex:
            return Result.error_result(message="移除任务失败，原因：%s" % str(ex))

    def find_job(self, job_id):
        try:
            res = scheduler.get_job(job_id=job_id, jobstore=self.job_store)
            return Result.success_result(data=res)
        except Exception as ex:
            return Result.error_result(message="查找任务失败，原因：%s" % str(ex))

    def pause_job(self, job_id):
        try:
            scheduler.pause_job(job_id=job_id, jobstore=self.job_store)
            return Result.success_result()
        except Exception as ex:
            return Result.error_result(message="暂停任务失败，原因：%s" % str(ex))

    def resume_job(self, job_id):
        try:
            scheduler.resume_job(job_id=job_id, jobstore=self.job_store)
            return Result.success_result()
        except Exception as ex:
            return Result.error_result(message="恢复任务失败，原因：%s" % str(ex))
